package cn.edu.hstc.wm.ui;

import java.text.SimpleDateFormat;
import java.util.Date;

import android.annotation.SuppressLint;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;
import cn.edu.hstc.wm.R;
import cn.edu.hstc.wm.service.FeedbackService;
import cn.edu.hstc.wm.ui.base.BaseActivity;
import cn.edu.hstc.wm.widget.CustomToast;

/**
 * @ClassName: UserFeedback
 * @Description: 用户反馈页面，
 * 
 * @ModificationHistory
 */
public class UserFeedback extends BaseActivity implements OnClickListener {

	private EditText mContactEdit = null;
	private EditText mContentEdit = null;
	private ImageView mBackView = null;
	private ImageView mRecordView = null;
	private Button mSubmitBtn = null;
	private SendFeedbackTask mTask = null;

	private String mTime = "";

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.user_feedback);
		initView();
		// 立即彈出鍵盤
		// InputMethodManager imm = (InputMethodManager)
		// getSystemService(Context.INPUT_METHOD_SERVICE);
		// imm.toggleSoftInput(InputMethodManager.RESULT_UNCHANGED_SHOWN,
		// InputMethodManager.HIDE_NOT_ALWAYS);
	}

	private void initView() {
		mBackView = (ImageView) findViewById(R.id.iv_feedback_back);
		mRecordView = (ImageView) findViewById(R.id.iv_feedback_record);

		mContactEdit = (EditText) findViewById(R.id.et_feedback_contact);
		mContentEdit = (EditText) findViewById(R.id.et_feedback_content);

		mSubmitBtn = (Button) findViewById(R.id.bt_feedback_submit);

		mBackView.setOnClickListener(this);
		mRecordView.setOnClickListener(this);
		mSubmitBtn.setOnClickListener(this);

	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.iv_feedback_back:
			// if (mTask != null) {
			// return;
			// }
			finish();
			break;

		case R.id.iv_feedback_record:
			// 查看反馈记录
			Intent intent = new Intent();
			intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
			intent.setClass(UserFeedback.this, UserFeedbackRecord.class);
			startActivity(intent);
			break;

		case R.id.bt_feedback_submit:
			// 提交反馈
			send();
			break;

		default:
			break;
		}
	}

	@SuppressLint("SimpleDateFormat")
	private void send() {
		String content = mContentEdit.getText().toString().trim();
		String userContack = mContactEdit.getText().toString().trim();

		if (TextUtils.isEmpty(content)) {
			CustomToast.make(this).setText("反馈内容不能为空").show();
			return;
		}
		if (TextUtils.isEmpty(userContack)) {
			CustomToast.make(this).setText("请输入联系方式").show();
			return;
		}

		// 添加时间记录
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
		mTime = sdf.format(new Date());

		mTask = new SendFeedbackTask();
		mTask.execute(content, userContack, mTime);
	}

	/**
	 * @ClassName: SendFeedbackTask
	 * @Description: 发送反馈到服务器
	 * 
	 * @ModificationHistory
	 */
	private class SendFeedbackTask extends AsyncTask<String, Integer, Integer> {

		private ProgressDialog mProgDialog = null;

		@Override
		protected Integer doInBackground(String... params) {
			// 反馈内容，反馈用户联系方式,时间
			Record record = new Record();
			record.setId(1);
			record.setTime(params[2]);
			record.setContact(params[1]);
			record.setContent(params[0]);
			
			//本地保存
			FeedbackService.saveRecord(UserFeedback.this, record);
			
			return FeedbackService.sendFeedbackMessage(record);
		}

		@Override
		protected void onPostExecute(Integer result) {
			if (mProgDialog != null) {
				mProgDialog.dismiss();
			}
			if (result == 0) {
				Toast.makeText(UserFeedback.this, R.string.feedback_success, Toast.LENGTH_SHORT).show();
				finish();
			} else {
				Toast.makeText(UserFeedback.this, R.string.feedback_failed, Toast.LENGTH_SHORT).show();
			}
			return;
		}

		@Override
		protected void onPreExecute() {
			mProgDialog = new ProgressDialog(UserFeedback.this);
			mProgDialog.setMessage(UserFeedback.this.getString(R.string.feedback_waiting));
			mProgDialog.setCancelable(false);
			mProgDialog.show();
		}

	}

	/**
	 * @ClassName: Record
	 * @Description: 記錄類
	 * 
	 * @ModificationHistory
	 */
	public static class Record {
		private int id;
		private String contact;
		private String content;
		private String time;

		public void setId(int id) {
			this.id = id;
		}

		public int getId() {
			return id;
		}

		public String getContact() {
			return contact;
		}

		public void setContact(String contact) {
			this.contact = contact;
		}

		public String getContent() {
			return content;
		}

		public void setContent(String content) {
			this.content = content;
		}

		public String getTime() {
			return time;
		}

		public void setTime(String time) {
			this.time = time;
		}

	}

}
